package my.sample.encryption;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
 
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
 
/**
 * A utility class that encrypts or decrypts a file.
 *
 */
public class CryptoUtils {
	
	// we can use AES or DES algorithms
    private static final String ALGORITHM = "DES";
    private static final String TRANSFORMATION = "DES";
 
    public static void encrypt(String key, File inputFile, File outputFile)
            throws Exception {
        doCrypto(Cipher.ENCRYPT_MODE, key, inputFile, outputFile);
    }
 
    public static void decrypt(String key, File inputFile, File outputFile)
            throws Exception {
        doCrypto(Cipher.DECRYPT_MODE, key, inputFile, outputFile);
    }
 
    private static void doCrypto(int cipherMode, String key, File inputFile,
            File outputFile) throws Exception {
        try {
            Key secretKey = new SecretKeySpec(key.getBytes(), ALGORITHM);
            Cipher cipher = Cipher.getInstance(TRANSFORMATION);
            cipher.init(cipherMode, secretKey);
             
            FileInputStream inputStream = new FileInputStream(inputFile);
            byte[] inputBytes = new byte[(int) inputFile.length()];
            inputStream.read(inputBytes);
             
            byte[] outputBytes = cipher.doFinal(inputBytes);
             
            FileOutputStream outputStream = new FileOutputStream(outputFile);
            outputStream.write(outputBytes);
             
            inputStream.close();
            outputStream.close();
             
        } catch (NoSuchPaddingException ex) {
            throw new Exception("Error encrypting/decrypting file", ex);
        } catch ( NoSuchAlgorithmException ex) {
          ex.printStackTrace();
        } catch (InvalidKeyException  ex) {
        	  ex.printStackTrace();
        } catch ( BadPaddingException ex) {
            ex.printStackTrace();
        } catch ( IllegalBlockSizeException ex) {
            ex.printStackTrace();
        }catch (IOException ex){
            ex.printStackTrace();
        }
    }
    
    
    public static void main (String args[]){
    	
    	// For AES algorithm : key length must be 16 bytes
    	// For DES algorithm : key length must be 8 bytes
/*    	encrypt
    	true plainfile encryptedfile password
    	
    	decrypt
    	false decryptedfile plainfile password*/
    	
    	System.out.println(Arrays.asList(args));
		boolean encrypt;
		
		File inputFile, encryptedFile = null, decryptedFile = null, outputFile;
		if (args[0].equalsIgnoreCase("encrypt")) {
			encrypt = true;
		} else {
			encrypt = false;
		}
		
		inputFile = new File(args[1]);
		outputFile = new File(args[2]);
    	 String key = args[3];
		
		if ( encrypt ) {
			
			
		} else {
			
			
		}
    	 
         
         
          
         try {
        	 
             CryptoUtils.encrypt(key, inputFile, encryptedFile);
             
             System.out.println("Encryption is done ..............");
             Thread.sleep(10000);
             CryptoUtils.decrypt(key, encryptedFile, decryptedFile);
             
             System.out.println("Decryption  is done ..............");
         } catch (Exception ex) {
             System.out.println(ex.getMessage());
             ex.printStackTrace();
         }
    }
}
